﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class DictColor : Form
    {
        public DictColor()
        {
            InitializeComponent();
        }

        // metoda podpięta pod event TextChange elementu txtElem - 
        //      odblokowuje przycisk "Dodaj"
        //
        // cel: uniemozliwienie dodawania pustego elementu do listy
        //
        private void txtElem_TextChanged(object sender, EventArgs e)
        {
            if (txtId.Text.Length > 0)
            {
                this.btnAdd.Enabled = false;
                this.btnEdit.Enabled = true;

                return;
            }

            if ((this.txtElem.Text != "") && (this.txtRGBHex.Text != ""))
                this.btnAdd.Enabled = true;
            else
                this.btnAdd.Enabled = false;
        }

        // metoda podpięta pod event Click elementu lstView - 
        //      odblokowuje przycisk "Usuń"
        //
        // cel: uniemożliwia usunięcie niezaznaczonego elementu
        //
        private void lstView_ItemSelected(object sender, EventArgs e)
        {
            //if (this.lstView.SelectedItem != null)
            //    this.btnDel.Enabled = true;

            if (lstView.SelectedValue as DictColorItem != null)
            {
                // Na starcie działa jako obiekt
            // Brak wypełniania przy starcie
            //    this.colorDialog1.Color = (lstView.SelectedValue as DictColorItem).Color;
            //    this.txtElem.Text = (lstView.SelectedValue as DictColorItem).DictValue;
            //    txtId.Text = (lstView.SelectedValue as DictColorItem).Id.ToString();
            }
            else if( lstView.SelectedValue != null )
            {
                // Później jako id
                this.colorDialog1.Color = ServiceFactory.getDictService().getColorById( (int)lstView.SelectedValue ).Color;
                this.txtElem.Text = ServiceFactory.getDictService().getColorById( (int)lstView.SelectedValue ).DictValue;
                txtId.Text = lstView.SelectedValue.ToString();
            }

            //btnEdit.Enabled = btnEdit.Visible = true;
            //btnAdd.Enabled = btnAdd.Visible = false;

            Refresh_Color();
            Refresh_Buttons();
        }

        // metoda podpięta pod event Click elementu btnDel -
        //      blokujący przycisk "Usuń"
        //
        // cel: po usunięciu zaznaczonego elementu focus z listboxa,
        //      w tym momencie możliwość usuwania powinna zostać zablokowana
        //
        private void btnDel_Click(object sender, EventArgs e)
        {
            //this.lstView.Items.Remove(this.lstView.SelectedItem);
            IList<DictColorItem> list = this.lstView.DataSource as List<DictColorItem>; 
            if (list != null)
            { 
                DictService ds = ServiceFactory.getDictService();
                DictColorItem item = new DictColorItem();
                item.Id = (int)lstView.SelectedValue;
                ds.deleteColor( item );

                Load_Colors();
            }

            //this.btnDel.Enabled = false;
            Refresh_Buttons();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if ( txtElem.Text.Length > 0 )
            {
                DictService ds = ServiceFactory.getDictService();
                DictColorItem item = new DictColorItem();
                item.DictValue = txtElem.Text;
                item.Color = colorDialog1.Color;
                int state = ds.addColor(item);

                if (state == DictService.ACTIVATED)
                    MessageBox.Show("Ten kolor był już w bazie danych i został ponownie aktywowany!");
                
                if (state == DictService.ERROR)
                    MessageBox.Show("Ten kolor jest już w bazie danych");


                Load_Colors();
            }
        }

        private void btnPal_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();

            Refresh_Color();

        }

        private void DictColor_Load(object sender, EventArgs e)
        {
            Load_Colors();
            btnEdit.Left = btnAdd.Left;
            btnEdit.Top = btnAdd.Top;
        }

        private void Load_Colors()
        {
            IList<DictColorItem> list = ServiceFactory.getDictService().getColors( new Filter("active", 1).asList() );

            lstView.SelectedItem = null;
            lstView.DataSource = list;
            lstView.DisplayMember = "DictValue";
            lstView.ValueMember = "Id";

            lstView.SelectedItem = null;
            txtId.Text = "";
            txtElem.Text = "";
            Refresh_Buttons();
        }
        private void Refresh_Color()
        {

            Color c1,c2;
            c1 = Color.FromArgb(255, colorDialog1.Color);
            c2 = Color.FromArgb(0, c1);
            colorDialog1.Color = c1;

            //txtRGBNum.Text = c2.ToArgb().ToString();
            txtRGBHex.Text = String.Format("#{0:X}{1:X}{2:X}{3:X}{4:X}{5:X}", c1.R % 16, c1.R / 16, c1.G % 16, c1.G / 16, c1.B % 16, c1.B / 16);

            txtRGBHex.BackColor = c1;
            if (c1.R + c1.G * 4 + c1.B > 700) txtRGBHex.ForeColor = Color.Black;
            else txtRGBHex.ForeColor = Color.White;

            /*
            Color c2 = new Color();
            Int32 argb = c1.ToArgb();
            c2 = Color.FromArgb(argb);
            //MessageBox.Show(colorDialog1.Color.ToArgb().ToString());
            //MessageBox.Show( argb.ToString()+ ": " +c2.ToString() );
            txtRGBHex.Text = argb.ToString();
            // */
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtElem.Text.Length > 0)
            {
                DictService ds = ServiceFactory.getDictService();
                DictColorItem item = new DictColorItem();
                item.DictValue = txtElem.Text;
                item.Color = colorDialog1.Color;
                item.Id = Convert.ToInt32( txtId.Text );
                int state = ds.updateColor(item);

                if (state == DictService.ERROR)
                    MessageBox.Show("Błąd aktualizacji koloru");

                Load_Colors();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            lstView.SelectedItem = null;
            txtId.Text = "";
            Refresh_Buttons();
        }

        private void Refresh_Buttons()
        {
            if (txtId.Text != "")
            {
                btnAdd.Visible = btnAdd.Enabled = false;
                btnEdit.Visible = btnEdit.Enabled = true;
                btnDel.Enabled = true;

                labelId.ForeColor = txtId.ForeColor = Color.Black;
            }
            else
            {
                btnAdd.Visible = true;
                btnAdd.Enabled = (txtElem.Text != "");
                btnEdit.Visible = btnEdit.Enabled = false;
                btnDel.Enabled = false;

                labelId.ForeColor = txtId.ForeColor = Color.Gray;
            }
        }


    }
}
